<%@ include file="/html/init.jsp" %>

<c:set var="rules" value="${requestScope.rules}"/>
<c:set var="redirect" value="${requestScope.backURL}"/>
<c:set var="action" value="${requestScope.action}" />
<c:set var="listModels" value="${requestScope.listModels}" />
<c:set var="listColumns" value="${requestScope.listColumns}" />

<script>
var portletNamespace = '<portlet:namespace/>';
var listColumnsAux = "<c:out value='${listColumns}'></c:out>";
var rowsCounter = 1;
</script>

<!-- <script src="../../light_ui_br_portlet-portlet/js/rulesJS.js"></script> -->
<!-- <script src="../../light_ui_br_portlet-portlet/js/jquery-1.10.2.js"></script> -->
<!-- <script src="../../light_ui_br_portlet-portlet/js/jquery.chained.js"></script> -->

<aui:model-context bean="${rules}" model="<%= rules.class %>" />

<c:choose>
	<c:when test="${rules != null}">
		<liferay-ui:header
		backURL="${redirect}"
		title="${rules.ruleName}"/>
	</c:when>
	<c:otherwise>
		<liferay-ui:header
		backURL="${redirect}"
		title='<%=LanguageUtil.get(pageContext, "new-rule") %>'/> 
	</c:otherwise>
</c:choose>

<c:choose>
	<c:when test="${rules == null}">
		<portlet:actionURL name="addRule" var="editRuleURL" windowState="normal" />
		<aui:form action="<%=editRuleURL%>" method="POST" name="fm">
		<aui:input type="hidden" name="redirect" value="${redirect}" />
		<c:choose>
			<c:when test="${rules == null} ">
				<aui:input type="hidden" name="ruleId" value=""/>
			</c:when>
			<c:otherwise>
				<aui:input type="hidden" name="ruleId" value="${rules.ruleId}"/>
			</c:otherwise>
		</c:choose>
		<c:choose>
			<c:when test="${rules == null}">
				<aui:input label='<%=LanguageUtil.get(pageContext,"rule-name")%>' inlineField="true"
							name="ruleName" type="textbox" id="ruleName"/>
			</c:when>
			<c:otherwise>
				<aui:input label='<%=LanguageUtil.get(pageContext, "rule-name") %>' inlineField="true"
							name="ruleName" type="textbox" value="${rules.ruleName}"/>
			</c:otherwise>
		</c:choose>
		<aui:select label='<%=LanguageUtil.get(pageContext, "rule-logicOp") %>' name='ruleTypeSelect1' id="ruleTypeSelect1"
							inlineField="true">
							<aui:option id='ruleTypeOption' value='AND'
								label='<%=LanguageUtil.get(pageContext,
										"rule-andRule")%>'
								selected='AND' />
							<aui:option id='ruleTypeOption' value='OR'
								label='<%=LanguageUtil.get(pageContext,
										"rule-orRule")%>' />
		</aui:select>
		
		<hr/>
						
			<aui:fieldset id="rulesFields" cssClass="rulesFields">
				<div class="lfr-form-row lfr-form-row-inline">
					<div class="row-fields">
						<aui:select
							label='<%=LanguageUtil.get(pageContext,
										"rule-modelId")%>'
							showEmptyOption="true" name='ruleModelSelect1'
							id="ruleModelSelect1" onChange="loadColumnsList(this)"
							inlineField="true">
							<c:forEach items="${listModels}" var="ruleModel">
								<c:if test="${ruleModel != selected}">
									<aui:option id='ruleModelOption' value='${ruleModel}'
										label='${ruleModel}' selected='${ruleModel}' />
								</c:if>
							</c:forEach>
						</aui:select>
						<aui:select
							label='<%=LanguageUtil.get(pageContext,
										"rule-ruleField")%>'
							name='ruleFieldSelect1' id="ruleFieldSelect1" disabled="true"
							showEmptyOption="true" onChange="fieldOption(this)"
							inlineField="true">
						</aui:select>
						<aui:select
							label='<%=LanguageUtil.get(pageContext,
										"rule-operator")%>'
							name='ruleOperator1' id="ruleOperator1" inlineField="true"
							showEmptyOption="true" disabled="true">
							<aui:option id='ruleOperatorSelected1' value="==" label="=="
								selected="==" />
							<aui:option id='ruleOperatorSelected2' value="!=" label="!="
								selected="!=" />
							<aui:option id='ruleOperatorSelected3' value="&lt;" label="&lt;"
								selected="&lt;" />
							<aui:option id='ruleOperatorSelected4' value=">" label=">"
								selected=">" />
							<aui:option id='ruleOperatorSelected5' value="<=" label="<="
								selected="<=" />
							<aui:option id='ruleOperatorSelected6' value=">=" label=">="
								selected=">=" />
							<aui:option id='ruleOperatorSelected7' value="contains"
								label='<%=LanguageUtil.get(pageContext,
										"rule-containsOperator")%>'
								selected="contains" />
							<aui:option id='ruleOperatorSelected8' value="notContains"
								label='<%=LanguageUtil.get(pageContext,
										"rule-notContainsOperator")%>'
								selected="notContains" />
						</aui:select>
						<c:choose>
							<c:when test="${rules == null}">
								<aui:input
									label='<%=LanguageUtil.get(pageContext,
											"rule-value")%>'
									name="ruleValue1" id="ruleValue1" type="textbox" value=""
									disabled="true" inlineField="true" />
								<div class="aui-datepicker aui-helper-clearfix"
									id="#<portlet:namespace />startDatePicker1">
									<input type="text" name="startDate1"
										id="startDate1" size="30" />
								</div>
							</c:when>
							<c:otherwise>
								<aui:input
									label='<%=LanguageUtil.get(pageContext, "rule-value") %>'
									name="ruleValue" type="textbox" value="${rules.ruleValue}" />
							</c:otherwise>
						</c:choose>
					</div>
				</div>
			</aui:fieldset>
			
			<aui:input name="rowsCounterControl" id="rowsCounterControl" type="hidden" value="1"></aui:input>

<aui:script use="liferay-auto-fields">
	var autoFields=new Liferay.AutoFields(
		{
			contentBox: 'fieldset.rulesFields',
			fieldIndexes: '<portlet:namespace />fieldset.rulesFieldsIndexes',
            on: {
                    'clone': function(event) {
                    	<!-- Funciones al anadir -->
                    	rowsCounter++;
                    	<!-- Creamos un nuevo calendario en cada fila -->
						AUI().use('aui-datepicker', function(A) {

						    var simpleDatepicker1 = new A.DatePicker({
								
								calendar: {
								          dateFormat: '%d/%m/%Y',
								        },
						
								trigger: '#startDate' + rowsCounter,
						
						       }).render('##<portlet:namespace />startDatePicker1');
						       document.getElementById('startDate' + rowsCounter).style.display = 'none';
						       });
						<!-- Definir todos los elementos deshabilitados para evitar conflictos -->
						document.getElementById("<portlet:namespace />ruleFieldSelect" + rowsCounter).disabled=true;
						document.getElementById("<portlet:namespace />ruleOperator" + rowsCounter).disabled=true;
						document.getElementById("<portlet:namespace />ruleValue" + rowsCounter).disabled=true;
						document.getElementById("<portlet:namespace />ruleValue" + rowsCounter).style.display = 'inherit';
						document.getElementById("<portlet:namespace />ruleValue" + rowsCounter).value = "";
						document.getElementById("<portlet:namespace />rowsCounterControl").value = rowsCounter;
				    
                    },
                    'delete': function(event) {
                        <!-- Funciones al borrar -->
                    } 
                    
            }
		}
	).render();

</aui:script>

<aui:script use="aui-datepicker">

    var simpleDatepicker1 = new A.DatePicker({
		
		calendar: {
		          dateFormat: '%d/%m/%Y',
		        },

		trigger: '#startDate' + rowsCounter,

       }).render('##<portlet:namespace />startDatePicker' + rowsCounter);
       
       document.getElementById('startDate' + rowsCounter).style.display = 'none';
       
</aui:script>

		<c:choose>
		<c:when test="${rules == null}">
			<aui:input label='<%=LanguageUtil.get(pageContext,
										"is-active")%>' name="active" type="checkbox" checked="false"/>
		</c:when>
		<c:otherwise>
			<aui:input label='<%=LanguageUtil.get(pageContext, "is-active") %>' name="active" type="checkbox" checked="${rules.active}"/>
		</c:otherwise>
		</c:choose>
	

			
		<aui:button-row>
			<aui:button type="submit" value='<%=LanguageUtil.get(pageContext, "accept")%>'  />
			<aui:button type="cancel" name='<%=LanguageUtil.get(pageContext, "cancel") %>' onClick="${redirect}" />
		</aui:button-row>
		</aui:form>
	</c:when>
	<c:otherwise>
		<portlet:actionURL name="updateRule" var="editRuleURL" windowState="normal" />
		<aui:form action="<%=editRuleURL%>" method="POST" name="fm">
		<aui:fieldset>
		<aui:input type="hidden" name="redirect" value="${redirect}" />
		<c:choose>
			<c:when test="${rules == null} ">
				<aui:input type="hidden" name="ruleId" value=""/>
			</c:when>
			<c:otherwise>
				<aui:input type="hidden" name="ruleId" value="${rules.ruleId}"/>
			</c:otherwise>
		</c:choose>
		<c:choose>
			<c:when test="${rules == null} ">
				<aui:input label='<%=LanguageUtil.get(pageContext,
											"rule-name")%>' type="textbox" name="ruleName" value=""/>
			</c:when>
			<c:otherwise>
				<aui:input label='<%=LanguageUtil.get(pageContext, "rule-name") %>' type="textbox" name="ruleName" value="${rules.ruleName}"/>
			</c:otherwise>
		</c:choose>
		<aui:select label='<%=LanguageUtil.get(pageContext,
										"rule-modelId")%>' name='ruleModel' id="ruleModelUpdate">
			<c:forEach items="${listModels}" var="ruleModel">
				<c:if test="${ruleModel != selected}">
					<aui:option value="${ruleModel}" label="${ruleModel}" selected="${ruleModel == rules.model}"/>
				</c:if>
			</c:forEach>
		</aui:select>
		<aui:select label='<%=LanguageUtil.get(pageContext,
										"rule-modelRule")%>' name='ruleField' id="ruleField">
			<c:forEach items="${listColumns}" var="ruleField">
				<c:if test="${ruleField != selected}">
					<aui:option value="${ruleField}" label="${ruleField}" selected="${ruleField == rules.model}"/>
				</c:if>
			</c:forEach>
		</aui:select>		
		<c:choose>
			<c:when test="${rules == null}">
				<aui:input label='<%=LanguageUtil.get(pageContext,
											"is-active")%>' name="active" type="checkbox" checked="false"/>
			</c:when>
			<c:otherwise>
				<aui:input label='<%=LanguageUtil.get(pageContext, "is-active") %>' name="active" type="checkbox" checked="${rules.active}"/>
			</c:otherwise>
		</c:choose>
	
			</aui:fieldset>

			
			<aui:button-row>
				<aui:button type="submit" value='<%=LanguageUtil.get(pageContext, "accept")%>'  />
				<aui:button type="cancel" name='<%=LanguageUtil.get(pageContext, "cancel") %>' onClick="${redirect}" />
			</aui:button-row>
		</aui:form>
	</c:otherwise>
</c:choose>

<script src="../../light_ui_br_portlet-portlet/js/rulesJS.js"></script>